package com.wisdomsky.project.param.service.logic.impl;

import com.alibaba.fastjson.JSONArray;
import com.wisdomsky.commonUtil.pojo.build.BuildResponse;
import com.wisdomsky.project.param.pojo.Params;
import com.wisdomsky.project.param.service.business.ParamsService;
import com.wisdomsky.project.param.service.logic.BaseService;
import com.wisdomsky.project.param.service.logic.ParamsLogicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParamsLogicServiceImpl extends BaseService implements ParamsLogicService {

    @Autowired
    private ParamsService paramsService;

    @Override
    public BuildResponse retrieveRoles(Params params, String... array) {
        return BuildResponse.builder().array(
                getParamsJSONArray(params.getType() == 1 ? paramsService.retrieveRoles() :
                        (params.getType() == 2 ? paramsService.retrieveProjectRoles() : paramsService.retrieveRoles_0()), array)).build();
    }

    @Override
    public BuildResponse retrieveSystem(String... array) {
        return BuildResponse.builder().array(getParamsJSONArray(paramsService.retrieveSystem(), array)).build();
    }

    @Override
    public BuildResponse retrieveRecurrent(String... array) {
        return BuildResponse.builder().array(getParamsJSONArray(paramsService.retrieveRecurrent(), array)).build();
    }

    @Override
    public BuildResponse retrieveRepairState(String... array) {
        return BuildResponse.builder().array(getParamsJSONArray(paramsService.retrieveRepairState(), array)).build();
    }

    @Override
    public BuildResponse retrieveProjectState(Params params, String... array) {
        return BuildResponse.builder().array(getParamsJSONArray(paramsService.retrieveProjectState(), array)).build();
    }

    private synchronized JSONArray getParamsJSONArray(List<Params> list, String... array){
        JSONArray jsonArray = new JSONArray();
        for (Params params:list){
            jsonArray.add(getJSONObj(params, array));
        }
        return jsonArray;
    }
}
